package com.dog.basics.netty.business.impl;

import cn.hutool.core.util.StrUtil;
import com.dog.basics.common.util.SpringContextUtils;
import com.dog.basics.netty.business.IBusinessServer;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import com.dog.basics.netty.util.SessionSocketHolder;
import com.dog.basics.service.mtkj.IMtDeviceLockService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.log4j.Log4j2;

/**
 * @description: 轮椅异常上报 (锁异常)
 * @author: He
 * @date: Created in 2020/10/12 18:11
 * @version: 1.0
 * @modified By:
 */
@Log4j2
public class ResponseHandlerImplWheelchairEx implements IBusinessServer<DogNewsContentDto.Message> {

    /**
     * @功能描述: 轮椅异常上报 (锁异常) - 执行
     * @author He
     * @date 2020/10/12
     */
    @Override
    public void businessHandler(ChannelHandlerContext ctx, DogNewsContentDto.Message msg) throws InterruptedException {
        log.info("轮椅异常上报 (锁异常) : {} 锁通道号 : {} 设备异常码 : {}", msg.toString(),msg.getLockport(),msg.getLockErroCode());
        // 查询锁标识
        Long dvId = SessionSocketHolder.getSession(ctx.channel().id().asLongText());
        // 轮椅异常上报 - 执行
        Boolean dataReruen = Boolean.FALSE;
        try {
            dataReruen = SpringContextUtils.getBean(IMtDeviceLockService.class).editAbnormalReport(dvId,msg.getLockport(), StrUtil.toString(msg.getLockErroCode()));
        } catch (Exception e) {
            log.error("轮椅异常上报-执行异常:[ 锁标识:{} 异常内容:{}]", dvId, e.getMessage());
        }
        // 返回 执行成功或失败
        ctx.channel().writeAndFlush(DogNewsContentDto.Message.newBuilder().setMsgType(msg.getMsgType()).setMsgCode(dataReruen ? 1 : 2).build());
    }

}
